﻿using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Little.Soldier.Licensing
{
  [DesignTimeVisible(false)]
  [ToolboxItem(false)]
  internal class LicenseInstallForm : Form
  {
    private bool isValid = true;
    private Button btnOK = new Button();
    protected TextBox KeyText;
    private LicenseInfo _LicenseInfo;
    private Container container1;
    internal Label Label3;
    private Label label5;
    internal Button btnGetKey;
    internal Label label1;
    private Panel panel1;
    private Button btnCancel;
    private Panel panel2;
    private Panel panel3;
    private Panel panel4;
    private Panel panel5;
    private Label label2;
    private System.Type _TypeToLicense;

    public System.Type TypeToLicense
    {
      get
      {
          return this._TypeToLicense;
      }
      set
      {
          this._TypeToLicense = value;
      }
    }

    public LicenseInfo InstalledLicense
    {
      get
      {
        return this._LicenseInfo;
      }
    }

    public LicenseInstallForm()
    {
        this.InitializeComponent();
    }

    private void InitializeComponent()
    {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(LicenseInstallForm));
            this.KeyText = new System.Windows.Forms.TextBox();
            this.Label3 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnGetKey = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnCancel = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.panel4 = new System.Windows.Forms.Panel();
            this.panel5 = new System.Windows.Forms.Panel();
            this.label2 = new System.Windows.Forms.Label();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // KeyText
            // 
            this.KeyText.Location = new System.Drawing.Point(12, 106);
            this.KeyText.Name = "KeyText";
            this.KeyText.Size = new System.Drawing.Size(416, 20);
            this.KeyText.TabIndex = 2;
            // 
            // Label3
            // 
            this.Label3.BackColor = System.Drawing.Color.Transparent;
            this.Label3.Location = new System.Drawing.Point(12, 90);
            this.Label3.Name = "Label3";
            this.Label3.Size = new System.Drawing.Size(72, 16);
            this.Label3.TabIndex = 13;
            this.Label3.Text = "License Key";
            // 
            // label5
            // 
            this.label5.BackColor = System.Drawing.Color.Transparent;
            this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.25F);
            this.label5.Location = new System.Drawing.Point(12, 129);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(300, 14);
            this.label5.TabIndex = 18;
            this.label5.Text = "For trial period leave license key blank.";
            // 
            // btnOK
            // 
            this.btnOK.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.btnOK.Location = new System.Drawing.Point(265, 162);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(80, 22);
            this.btnOK.TabIndex = 20;
            this.btnOK.Text = "OK";
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnGetKey
            // 
            this.btnGetKey.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnGetKey.Location = new System.Drawing.Point(12, 162);
            this.btnGetKey.Name = "btnGetKey";
            this.btnGetKey.Size = new System.Drawing.Size(117, 22);
            this.btnGetKey.TabIndex = 22;
            this.btnGetKey.Text = "Get Free Trial Key";
            this.btnGetKey.Visible = false;
            this.btnGetKey.Click += new System.EventHandler(this.btnGetKey_Click);
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.Color.Transparent;
            this.label1.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
            this.label1.ForeColor = System.Drawing.Color.White;
            this.label1.Location = new System.Drawing.Point(45, 19);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(386, 19);
            this.label1.TabIndex = 23;
            this.label1.Text = "Little Soldier .NET is currently not licensed";
            // 
            // panel1
            // 
            this.panel1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("panel1.BackgroundImage")));
            this.panel1.Controls.Add(this.label1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(445, 43);
            this.panel1.TabIndex = 24;
            // 
            // btnCancel
            // 
            this.btnCancel.Cursor = System.Windows.Forms.Cursors.Default;
            this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnCancel.Location = new System.Drawing.Point(351, 162);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(80, 22);
            this.btnCancel.TabIndex = 21;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // panel2
            // 
            this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(483, 1);
            this.panel2.TabIndex = 25;
            // 
            // panel3
            // 
            this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel3.Location = new System.Drawing.Point(0, 194);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(483, 1);
            this.panel3.TabIndex = 26;
            // 
            // panel4
            // 
            this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel4.Location = new System.Drawing.Point(0, 0);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(1, 195);
            this.panel4.TabIndex = 27;
            // 
            // panel5
            // 
            this.panel5.BackColor = System.Drawing.Color.Transparent;
            this.panel5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel5.Location = new System.Drawing.Point(444, 0);
            this.panel5.Name = "panel5";
            this.panel5.Size = new System.Drawing.Size(1, 195);
            this.panel5.TabIndex = 28;
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.Location = new System.Drawing.Point(12, 56);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(419, 28);
            this.label2.TabIndex = 29;
            this.label2.Text = "Little Soldier .NET is currently not licensed. To install license, enter key you " +
    "received on purchasing.";
            // 
            // LicenseInstallForm
            // 
            this.AcceptButton = this.btnOK;
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.WhiteSmoke;
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.CancelButton = this.btnCancel;
            this.ClientSize = new System.Drawing.Size(445, 195);
            this.ControlBox = false;
            this.Controls.Add(this.label2);
            this.Controls.Add(this.panel5);
            this.Controls.Add(this.panel4);
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.Label3);
            this.Controls.Add(this.KeyText);
            this.Controls.Add(this.btnOK);
            this.Controls.Add(this.btnGetKey);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "LicenseInstallForm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Little Soldier .NET not licenced";
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
      base.OnPaintBackground(pevent);
      LinearGradientBrush linearGradientBrush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), System.Drawing.Color.LightGray, System.Drawing.Color.White, LinearGradientMode.ForwardDiagonal);
      pevent.Graphics.FillRectangle((Brush) linearGradientBrush, 0, 0, this.Width, this.Height);
    }

    protected override void OnClosing(CancelEventArgs e)
    {
      e.Cancel = !this.isValid;
      base.OnClosing(e);
    }

    public LicenseInfo GetLicenseInfo(string productName, System.Type typeToLicense)
    {
        this._TypeToLicense = typeToLicense;
      int num = (int) this.ShowDialog();
      return this._LicenseInfo;
    }

    protected override void Dispose(bool disposing)
    {
      if (disposing && this.container1 != null)
        goto label_2;
label_1:
      base.Dispose(disposing);
      return;
label_2:
      this.container1.Dispose();
      goto label_1;
    }

        protected virtual EncryptionProvider EncryptionProvider()
        {
            return new EncryptionProvider();
        }

        protected virtual bool Validate(string key)
        {
            try
            {
                LicensingManager.InitLicenseParameters();
                string str = key.Substring(0, 13);
                key = key.Substring(13);
                this._LicenseInfo = this.EncryptionProvider().GetLicenseInfo(key);
                if (this._LicenseInfo == null)
                {
                    int num = (int) MessageBox.Show("Specified license key is not a valid license key.", "License key error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                try
                {
                    RegistryKey registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\LittleSoldier\\Components\\LittleSoldier_2012.1", true);
                    if (str.Contains("-OL-"))
                        registryKey.SetValue("LicenseType", (object) "Single license");
                    if (str.Contains("-FL-"))
                        registryKey.SetValue("LicenseType", (object) "4 Developers license");
                    if (str.Contains("-SL-"))
                        registryKey.SetValue("LicenseType", (object) "Site license");
                }
                catch
                {
                }
                return this._LicenseInfo != null;
            }
            catch
            {
                int num = (int) MessageBox.Show("Specified license key is not a valid license key.", "License key error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            return false;
        }

    protected virtual void btnOK_Click(object sender, EventArgs e)
    {
      switch (this.KeyText.Text)
      {
        case null:
          this.Close();
          break;
        default:
          if (this.KeyText.Text.Trim().Length != 0)
          {
              if (!this.Validate(this.KeyText.Text))
              break;
            this.Close();
            break;
          }
          else
            goto case null;
      }
    }

    private void OpenIE(object sender, LinkLabelLinkClickedEventArgs e)
    {
      try
      {
        Process.Start("iexplore.exe", "http://www.littlesoldier.com/links/buy.aspx?id=USS&ref=Doc&license=OL");
      }
      catch
      {
      }
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void btnGetKey_Click(object sender, EventArgs e)
    {
      Process.Start("http://www.littlesoldier.com/links/get_free_trial_key.aspx");
    }

    protected override void OnPaint(PaintEventArgs e)
    {
      base.OnPaint(e);
      if (this.BackgroundImage == null)
        return;
      e.Graphics.DrawImage(this.BackgroundImage, 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height);
    }
  }
}
